/*************************************************************************
*
*	gotemp.c
*
*  Copyright 2007,2008,2009 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*
*  This module provides functions for simulating air temperature
*
*
**************************************************************************
*
*  version 1.0.2, 4/24/09, SBS
*    - documentation
*
*  version 1.0.1, 1/30/09, SBS
*    - add get_gotemp_K
*    - add fclose(fp) !
*
*  version 1.0, 1/29/09, SBS
*    - first integrated version, based on gotemp_standalone.c
*
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @defgroup plat_sci_hardware
*	@ingroup plat_science
*  Drivers for science-data reading hardware.
*/
/**
*  @file 
*  @ingroup plat_sci_hardware
*  @brief
*  This module provides functions for simulating air temperature
*  <hr>
**/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include "waypoints.h"
#include "platform_types.h"
#include "platform_modes.h"
#include "status.h"

#ifdef DEBUG
#include "logfile.h"
#endif

#include "gotemp.h"


/**
***************************************************************************
*
* get_gotemp_degC()
*/
/** 
*
* @brief
* Returns most recent temperature reading in *C
*
*
**************************************************************************/
static double get_gotemp_degC(
		char *port)
{
	double rval;
	uint8_t i[8];
//	char port[32] = "/dev/ldusb0";
	
	FILE *fp;
	
	fp = fopen(port,"rb");
	
	if(fp==NULL){
		printf("failed to open %s\n",port);
		return(-255.);
	}
	
	fread(i,sizeof(uint8_t),8,fp);
	
//	{
//	register int j;
	
//	for(j=0;j<8;j++){
//		printf("i[%d] = %d\n",j,i[j]);
//	}
//	}
	
	{
	uint16_t k;
	uint8_t  *m;
	
	m = (uint8_t *)&k;
	
	*m = i[4];
	*(m+1) = i[3];
	
	rval = (double)k/128;
//	printf("temp = %.1f *C\n",(double)k/128);
//	printf("temp = %.1f *F\n",(double)k/128*9/5+32);

	}
	
	fclose(fp);
	
	return(rval);
}


/**
***************************************************************************
*
* get_gotemp_K()
*/
/** 
*
* @brief
* Returns most recent temperature reading in K
*
*
**************************************************************************/
static double get_gotemp_K(
		char *port)
{
	double rval;
	
	rval = get_gotemp_degC(port) + 273.15;
		
	return(rval);
}


/**
***************************************************************************
*
* gotemp_GetSciDbl()
*/
/** 
*
* @brief
* Returns SCI_DBL with latest temperature
*
*
**************************************************************************/
SCI_DBL gotemp_GetSciDbl(
		char *port
		,POSITION p 
		, time_t t)
{
	struct tm *timeinfo;	
	SCI_DBL rval;	

	#ifdef DEBUG
	char logtemp[LOGSTRLEN];
	#endif

	rval.dValue = get_gotemp_K(port);
	snprintf(rval.sUnits,sizeof(rval.sUnits),"K");
	
	timeinfo = localtime(&t);
	status_SetTimeString(rval.sTime, sizeof(rval.sTime),timeinfo);
	
	rval.position = p;
	
	#ifdef DEBUG
	snprintf(logtemp,LOGSTRLEN
			,"***** gotemp_GetSciDbl() - temp = %f *C"
			,rval.dValue);
			
	logfile_LogLevelPrint(LOGLEVEL_INFO,logtemp);
	#endif

	return(rval);
}



